package com.library.service.impl;

import com.library.Mapper.LoginMapper;
import com.library.pojo.Admin;
import com.library.pojo.User;
import com.library.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public Admin login(Admin admin) {
        String password = admin.getPassword();
        String userName = admin.getUserName();
        Admin admin1 = loginMapper.loginForAdmin(userName);
        if(admin1 == null){
            throw  new RuntimeException("用户不存在");
        }
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!(admin1.getPassword().equals(md5))){
            throw  new RuntimeException("密码错误");
        }
        return admin1;
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public User userLogin(User user) {
        String password = user.getPassword();
        String userName = user.getUserName();
        User user1 = loginMapper.getUser(userName);
        if(user1==null){
            throw  new RuntimeException("用户不存在");
        }
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!user1.getPassword().equals(md5)){
            throw  new RuntimeException("密码错误");
        }
        return user1;
    }
}
